package org.skydiveapplibrary.repository;

import org.skydiveapplibrary.data.AltitudeUnit;
import org.skydiveapplibrary.data.Constants;
import org.skydiveapplibrary.data.UnitOfMeasure;
import org.skydiveapplibrary.data.WeightUnit;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class SettingsRepository
{
	private SharedPreferences mSharedPreferences;
	private Context mContext;
	
	public SettingsRepository(Context context)
	{
		mContext = context;
		mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
	}
	
	public void ReloadSettings()
	{
		mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
	}
	
	public UnitOfMeasure getUnitOfMeasure()
	{
		return UnitOfMeasure.valueOf(mSharedPreferences.getString(Constants.PREF_UNIT_OF_MEASURE, "US"));
	}
	
	public void setUnitOfMeasure(UnitOfMeasure unitOfMeasure)
	{
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putString(Constants.PREF_UNIT_OF_MEASURE, unitOfMeasure.name());
		editor.commit();
	}
	
	public Integer getExitAltitude()
	{
		return mSharedPreferences.getInt(Constants.PREF_EXIT_ALT, 0);
	}
	
	public void setExitAltitude(Integer exitAltitude)
	{
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putInt(Constants.PREF_EXIT_ALT, exitAltitude);
		editor.commit();
	}
	public Integer getDeploymentAltitude()
	{
		return mSharedPreferences.getInt(Constants.PREF_DEPLOY_ALT, 0);
	}
	public void setDeploymentAltitude(Integer deploymentAltitude)
	{
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putInt(Constants.PREF_DEPLOY_ALT, deploymentAltitude);
		editor.commit();
	}
	
	public WeightUnit getWeightUnit()
	{
		return (getUnitOfMeasure() == UnitOfMeasure.US) ?
				WeightUnit.Pounds : WeightUnit.Kilograms;
	}
	
	public AltitudeUnit getAltitudeUnit()
	{
		return (getUnitOfMeasure() == UnitOfMeasure.US) ?
				AltitudeUnit.Feet : AltitudeUnit.Meters;
	}
	
	public boolean getUseLightTheme()
	{
		return mSharedPreferences.getString(Constants.PREF_THEME, "Dark").equals("Light") ? true : false;
	}
	public void setUseLightTheme(boolean useLightTheme)
	{
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putString(Constants.PREF_THEME, useLightTheme ? "Light" : "Dark");
		editor.commit();
	}
	
	public boolean getUseExternalStorage()
	{
		return mSharedPreferences.getString(Constants.PREF_STORAGE_LOCATION, "Internal").
				equals("External") ? true : false;
	}
	public void setUseExternalStorage(boolean useExternalStorage)
	{
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putString(Constants.PREF_STORAGE_LOCATION, useExternalStorage ? "External" : "Internal");
		editor.commit();
	}
	
	public int getCutaways()
	{
		return mSharedPreferences.getInt(Constants.PREF_CUTAWAYS, 0);
	}
	public void setCutaways(int cutaways)
	{
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putInt(Constants.PREF_CUTAWAYS, cutaways);
		editor.commit();
	}
	
	public int getFreefallTime()
	{
		return mSharedPreferences.getInt(Constants.PREF_FREEFALLTIME, 0);
	}
	public void setFreefallTime(int freefallTime)
	{
		SharedPreferences.Editor editor = mSharedPreferences.edit();
		editor.putInt(Constants.PREF_FREEFALLTIME, freefallTime);
		editor.commit();
	}
}
